package com.hejing.project03_优尚开发团队人员调度系统.service;

import com.hejing.project03_优尚开发团队人员调度系统.domain.*;

/**
 * ClassName: NameListService
 * Package: com.hejing.project03_优尚开发团队人员调度系统.service
 * Description:
 *      负责将Data中的数据封装到 Employee[]数组中，同时提供相关操作Employee[] 的方法。
 * @Author: HeJing
 * @Create: 2023-04-13 18:41
 * @Version: 1.0
 */
public class NameListService {
    private Employee[] employees;

    public NameListService(){
        /**
         *  根据项目提供的Data类构建相应大小的employee数组，再根据Data类中的数据构建不同的对象，包括 Employee、Programmer
         *  Architect对象，以及相关联的Equipment子类的对象。
         *  将对象存于数组中
         */
        employees = new Employee[Data.EMPLOYEES.length];

        for(int i = 0; i < employees.length; i++){
            int type = Integer.parseInt(Data.EMPLOYEES[i][0]);  // 员工类型

            // 获取通用属性: id, name, age, salary
            int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
            String name = Data.EMPLOYEES[i][2];
            int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
            double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
            // 非通用属性定义
            Equipment equipment;
            double bonus;

            // 获取 bonus: (Designer, Architect 共用

            // 获取 stock:

            switch(type){
                case Data.EMPLOYEE:     // 员工
                    // 构造员工对象
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case Data.PROGRAMMER:   // 程序员
                    // 获取程序员特定的属性: equipment
                    equipment = getEquipment(i);
                    // 构造程序员对象
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case Data.DESIGNER:     // 设计师
                    // 获取equipment:
                    equipment = getEquipment(i);
                    // 获取设计师特定属性: bonus
                    bonus = getBonus(i);
                    // 构造设计师对象
                    employees[i] = new Designer(id, name, age, salary, equipment, bonus);
                    break;
                case Data.ARCHITECT:    // 架构师
                    // 获取equipment:
                    equipment = getEquipment(i);
                    // 获取设计师特定属性: bonus
                    bonus = getBonus(i);
                    // 获取架构师特定属性: stock
                    int stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
                    // 构造架构师对象。
                    employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
                    break;
            }
        }
    }

    /**
     *  获取指定下标位置的equipment属性(有的对象有，有的对象没有)
     *  获取 equipment:  (Programmer, Designer, Architect 共用)
     * @param i
     * @return
     */
    public static Equipment getEquipment(int i){
        Equipment equipment = null;
        if(Data.EQIPMENTS[i].length > 0) {
            int equipmentType = Integer.parseInt(Data.EQIPMENTS[i][0]);
            switch (equipmentType){
                case Data.PC:
                    equipment = new PC(Data.EQIPMENTS[i][1], Data.EQIPMENTS[i][2]);
                    break;
                case Data.NOTEBOOK:
                    equipment = new NoteBook(Data.EQIPMENTS[i][1], Double.parseDouble(Data.EQIPMENTS[i][2]));
                    break;
                case Data.PRINTER:
                    equipment = new Printer(Data.EQIPMENTS[i][1], Data.EQIPMENTS[i][2]);
                    break;
            }
        }
        return equipment;
    }

    /**
     * 获取指定下标位置 的 bonus属性
     * @param i
     * @return
     */
    public static double getBonus(int i){
        double bonus = 0;
        if(Data.EMPLOYEES[i].length > 5){
            bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
        }
        return bonus;
    }

    /**
     * 获取当前所有员工
     * @return
     */
    public Employee[] getAllEmployees(){
        return employees;
    }

    /**
     * 找到指定 id 的员工
     * @param id
     * @return
     */
    public Employee getEmployee(int id) throws TeamException{
        for(int i = 0; i < employees.length; i++){
            if(id == employees[i].getId()){
                return employees[i];
            }
        }
        throw new TeamException("查无此人!");
    }

}





























